package com.td.framework.webmanager.listener;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

//import com.td.framework.webmanager.log.action.DlrzAction;
import com.td.framework.webmanager.login.UserInfo;
/**
 * 
 * Title: 在线用户统计
 * Description:在线用户统计
 * Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 *
 * @author FLT007
 * @version 1.0
 */
@SuppressWarnings("serial")
public class OnlineUserInfo implements HttpSessionBindingListener {
	 
	ServletContext application = null;
	private  static  Vector  onlineUserV =  null; 
	
	public OnlineUserInfo(ServletContext application){//构造函数
		super();
		if(application==null){
			throw new IllegalArgumentException("Null application is not accept.");
		}
		this.application=application;
	}
	
	 
	/**
	 * 对象实例(即OnlineUserCounterListener的实例)作为一个属性被设置到session的时候，会调用本方法，这种情况一般发生在点击登录按钮以后的处理过程中设置
	 */
	@SuppressWarnings("unchecked")
	public void valueBound(HttpSessionBindingEvent arg0) {
		try {   
			HttpSession session = arg0.getSession();
		     onlineUserV = (Vector) application.getAttribute("onlineUserV");

		    UserInfo onlineUser = (UserInfo)session.getAttribute(session.getId());
		    if(onlineUser!=null){
		    	if(onlineUserV==null){
		    		onlineUserV = new Vector();
		    	}
		    	onlineUserV.add(onlineUser);
		    }
    		
		    application.setAttribute("onlineUserV", onlineUserV);
        } catch (RuntimeException e) { 
            e.printStackTrace(); 
        } 

	}
	/**
	 * 当Session超时，或本实例被从session中移除的时候被调用，这种情况一般发生在注销方法的处理过程中
	 */
	@SuppressWarnings("unchecked")
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		  try { 
			    HttpSession session = arg0.getSession();
			    UserInfo onlineUser = (UserInfo)session.getAttribute(session.getId());
		    	 onlineUserV = (Vector) application.getAttribute("onlineUserV");
		    	if(onlineUser!=null){
		    		if(onlineUserV!=null){	
		        		WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(application);
		        		//DlrzAction dlrzAction = (DlrzAction)ctx.getBean("dlrzAction");
		        		//dlrzAction.reworkDlrz(onlineUser.getYhbh());
				    	application.setAttribute("onlineUserV", onlineUserV);
				    	
		    			onlineUserV.remove(onlineUser);
		    		}		    		
		    	}
	        } catch (RuntimeException e) { 
	            e.printStackTrace(); 
	        } 
	    } 
	    public static void main(String[] args){

	    }
}
